package com.thwj.project.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thwj.common.exception.ServiceException;
import com.thwj.project.biz.domain.User;
import com.thwj.project.biz.domain.UserAccount;
import com.thwj.project.biz.domain.UserAccountInout;
import com.thwj.project.biz.domain.UserAccountWithdraw;
import com.thwj.project.biz.enums.AccountInoutStatusEnum;
import com.thwj.project.biz.enums.AccountSourceTypeEnum;
import com.thwj.project.biz.enums.AuditStatusEnum;
import com.thwj.project.biz.mapper.UserAccountWithdrawMapper;
import com.thwj.project.biz.service.IUserAccountInoutService;
import com.thwj.project.biz.service.IUserAccountService;
import com.thwj.project.biz.service.IUserAccountWithdrawService;
import com.thwj.project.biz.service.IUserService;
import com.thwj.project.push.enums.PushTemplateEnumNew;
import com.thwj.project.push.event.WxMpMessageEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户提现Service业务层处理
 *
 * @author generate
 * @date 2023-06-05
 */
@Transactional
@Service
public class UserAccountWithdrawServiceImpl extends ServiceImpl<UserAccountWithdrawMapper, UserAccountWithdraw> implements IUserAccountWithdrawService {

    @Autowired
    ApplicationEventPublisher applicationEventPublisher;
    @Autowired
    IUserAccountInoutService userAccountInoutService;
    @Autowired
    IUserAccountService userAccountService;
    @Autowired
    IUserService userService;

    @Override
    public int auditPass(UserAccountWithdraw userAccountWithdraw) {
        // 1: 审核通过 先更新 提现记录主记录
        UserAccountWithdraw current = this.getById(userAccountWithdraw.getId());
        if(current == null || current.getStatus() == null) {
            throw new ServiceException("参数不合法");
        }
        current.setStatus(AuditStatusEnum.auditing_success.getValue());
        current.setRemark(userAccountWithdraw.getRemark());
        this.updateById(current);
        // 2: 更新关联的出入账明细记录
        UserAccountInout userAccountInout = userAccountInoutService.getOne(
                new QueryWrapper<UserAccountInout>()
                        .eq("source_type", AccountSourceTypeEnum.withdraw.getValue())
                        .eq("source_id", current.getId()),
                false);
        if(userAccountInout == null) {
            throw new ServiceException("联系开发处理，你自己解决不了");
        }
        userAccountInout.setStatus(AccountInoutStatusEnum.Received.getValue());
        userAccountInoutService.updateById(userAccountInout);

        User user = userService.getById(current.getUserId());

        // 3: 发送模板消息通知用户

        // 发送微信通知给审核人员
        applicationEventPublisher.publishEvent(WxMpMessageEvent.builder()
                .pushTemplateEnum(PushTemplateEnumNew.WITHDRAW)
                .miniAppPath("/pages/my-wallet/my-wallet")
                .userId(userAccountWithdraw.getUserId())
                .templateDataList(PushTemplateEnumNew.WITHDRAW(
                                user.getUserNickName(),
                        current.getAmount(),
                        current.getActualAmount(),
                                "线下转账",
                                "已到账"
                        )
                ).build()
        );

//        applicationEventPublisher.publishEvent(
//                WxMpMessageEvent.builder()
//                        .pushTemplateEnum(PushTemplateEnum.WITHDRAW_FINISH)
//                        .miniAppPath("/pages/my-wallet/my-wallet")
//                        .templateDataList(
//                                PushTemplateEnum.getWITHDRAW_FINISH_Data(
//                                        userAccountWithdraw.getCreateDate(),
//                                        userAccountWithdraw.getRemark(),
//                                        userAccountWithdraw.getAmount(),
//                                        userAccountWithdraw.getCommission(),
//                                        userAccountWithdraw.getActualAmount())
//                        )
//                        .userId(userAccountWithdraw.getUserId())
//                        .build()
//        );
        return 1;
    }

    @Override
    public int auditNotPass(UserAccountWithdraw userAccountWithdraw) {
        // 1: 审核不通过 先更新 提现记录主记录
        UserAccountWithdraw current = this.getById(userAccountWithdraw.getId());
        if(current == null || current.getStatus() == null) {
            throw new ServiceException("参数不合法");
        }
        current.setStatus(AuditStatusEnum.auditing_fail.getValue());
        current.setRemark(userAccountWithdraw.getRemark());
        if(!this.updateById(current)) {
            throw new ServiceException("数据更新无效");
        }
        // 2: 更新关联的出入账明细记录
        UserAccountInout userAccountInout = userAccountInoutService.getOne(
                new QueryWrapper<UserAccountInout>()
                        .eq("source_type", AccountSourceTypeEnum.withdraw.getValue())
                        .eq("source_id", current.getId()),
                false);
        if(userAccountInout == null) {
            throw new ServiceException("联系开发处理，你自己解决不了");
        }
        userAccountInout.setStatus(AccountInoutStatusEnum.un_Received.getValue());
        userAccountInoutService.updateById(userAccountInout);
        // 3：将金额退回至原账户
        userAccountService.update(new UpdateWrapper<UserAccount>().setSql("amount = amount + "+ userAccountWithdraw.getAmount()).eq("user_id", current.getUserId()));

        // 4: 发送模板消息通知用户

        User user = userService.getById(current.getUserId());

        // 3: 发送模板消息通知用户

        // 发送微信通知给审核人员
        applicationEventPublisher.publishEvent(WxMpMessageEvent.builder()
                .pushTemplateEnum(PushTemplateEnumNew.WITHDRAW)
                .miniAppPath("/pages/my-wallet/my-wallet")
                .userId(userAccountWithdraw.getUserId())
                .templateDataList(PushTemplateEnumNew.WITHDRAW(
                                user.getUserNickName(),
                                current.getAmount(),
                                current.getActualAmount(),
                                "线下转账",
                                "已拒绝"
                        )
                ).build()
        );

//        applicationEventPublisher.publishEvent(
//                WxMpMessageEvent.builder()
//                        .pushTemplateEnum(PushTemplateEnum.WITHDRAW_FINISH)
//                        .miniAppPath("/pages/my-wallet/my-wallet")
//                        .templateDataList(
//                                PushTemplateEnum.getWITHDRAW_FINISH_Data(
//                                        userAccountWithdraw.getCreateDate(),
//                                        userAccountWithdraw.getRemark(),
//                                        userAccountWithdraw.getAmount(),
//                                        userAccountWithdraw.getCommission(),
//                                        userAccountWithdraw.getActualAmount())
//                        )
//                        .userId(userAccountWithdraw.getUserId())
//                        .build()
//        );
        return 1;
    }
}
